﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WTSQCB
{
    public partial class TieuDoan : System.Web.UI.Page
    {
        private string chuoiketnoi = ConfigurationManager.ConnectionStrings["WebDiemTSQCBConnectionString"].ConnectionString;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
         
        protected void ThemButton1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "TieuDoanThem";
                cmd.Parameters.AddWithValue("@matieudoan", MaTieuDoanTextBox1.Text);
                cmd.Parameters.AddWithValue("@tentieudoan", TenTieuDoanTextBox2.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                TieuDoanGridView1.DataBind();
                Response.Write("Thêm thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
            

        }

        protected void SuaButton1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "TieuDoanSua";
                cmd.Parameters.AddWithValue("@matieudoan", MaTieuDoanTextBox1.Text);
                cmd.Parameters.AddWithValue("@tentieudoan", TenTieuDoanTextBox2.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                TieuDoanGridView1.DataBind();
                Response.Write("Sửa thành công");
            }
            catch 
            {
                Response.Write("không thực hiện được");
            }
            
        }

        protected void XoaButton2_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "TieuDoanXoa";
                //cmd.Parameters.AddWithValue("@matieudoan", TieuDoanGridView1.SelectedRow.Cells[1].Text);
                cmd.Parameters.AddWithValue("@matieudoan", MaTieuDoanTextBox1.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                TieuDoanGridView1.DataBind();
                Response.Write("Xóa thành công");
            }
            catch (Exception ex)
            {
                Response.Write("không thực hiện được");
            }
            
            
        }
        KetNoiDuLieu kn = new KetNoiDuLieu();
        protected void TieuDoanGridView1_SelectedIndexChanged(object sender, EventArgs e)
        {                    
            GridViewRow dong = TieuDoanGridView1.SelectedRow;
            //MaTieuDoanTextBox1.Text = Convert.ToString(kn.LoadData("TieuDoanLoad").Rows[TieuDoanGridView1.SelectedIndex].ItemArray[0]);
            //TenTieuDoanTextBox2.Text = Convert.ToString(kn.LoadData("TieuDoanLoad").Rows[TieuDoanGridView1.SelectedIndex].ItemArray[1]);
            MaTieuDoanTextBox1.Text = HttpUtility.HtmlDecode(dong.Cells[1].Text);
            TenTieuDoanTextBox2.Text = HttpUtility.HtmlDecode(dong.Cells[2].Text);
        }

        protected void HuyButton1_Click(object sender, EventArgs e)
        {
            Page_Load(sender, e);
        }
        
    }
}